package com.jxc.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/ua")
public class RequestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String useragent = req.getHeader("User-Agent");
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().print(useragent);
        String output = "";
        // indexOf()方法，对字符串进行查找
        if (useragent.indexOf("Windows NT")!= -1){
            output = "<h1>这是PC端网页</h1>";
        }else if (useragent.indexOf("iPhone") != -1 || useragent.indexOf("Android") != -1){
            output = "<h1>这是手机端网页</h1>";
        }
        resp.getWriter().print(output);
    }
}
